package ${org}.${project}.common.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ${org}.${project}.common.model.${modelName?cap_first};
import ${org}.${project}.common.service.${modelName?cap_first}Service;
import ${org}.${project}.common.model.GridResponse;
import ${org}.${project}.common.model.ReturnModel;
import ${org}.${project}.common.enums.StatusEnum;
import ${org}.${project}.common.model.ReturnModel;

/**
 * ${modelComment !}
 * 
 * @author CodeGen
 *
 */
@Controller
@RequestMapping("/uc/${modelName?uncap_first}")
public class ${modelName?cap_first}Controller extends BaseController  {
	private final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private ${modelName?cap_first}Service ${modelName?uncap_first}Service;
	

	@RequestMapping(value="/index")
	public String ${modelName?uncap_first}(){
		return "uc/${modelName?uncap_first}/index";
	}

	
	/**
	 * 添加${modelComment !}
	 */
	@RequestMapping(value = "/add")
	public void add(${modelName?cap_first} ${modelName?uncap_first}) {
		JSONObject jsonObject = new JSONObject();
		try {
		    if (${modelName} != null) {
			${modelName?uncap_first}Service.add(${modelName?uncap_first});
		    }
		    jsonObject.put("sortMsg", "操作成功");
		    jsonObject.put("state", StatusEnum.STATUS_SUCCESS.getIndex());
		} catch (Exception e) {
		    logger.error(e.getMessage(), e);
		    jsonObject.put("sortMsg", "操作失败");
		    jsonObject.put("state", StatusEnum.STATUS_ERROR.getIndex());
		} finally {
		    try {
			printPageMsg(response, jsonObject.toJSONString());
		    } catch (Exception e) {
			logger.error(e.getMessage(), e);
		    }
		}
	}
	
	/**
	 * 删除${modelComment !}
	 */
	@RequestMapping(value = "/delete")
	public void delete(String idsStr) {
		JSONObject jsonObject = new JSONObject();
		try {
			if (StringUtils.isNotBlank(idsStr)) {
				String[] ids = idsStr.split(",");
				if (ids != null && ids.length > 0) {
					if (ids.length == 1) {
						${modelName?uncap_first}Service.deleteById(ids[0]);
					}else{
						${modelName?uncap_first}Service.delete(ids);
					}
				}
			}
	
			jsonObject.put("sortMsg", "操作成功");
			jsonObject.put("state", StatusEnum.STATUS_SUCCESS.getIndex());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			jsonObject.put("sortMsg", "操作失败");
			jsonObject.put("state", StatusEnum.STATUS_ERROR.getIndex());
		} finally {
			try {
			printPageMsg(response, jsonObject.toJSONString());
			} catch (Exception e) {
			logger.error(e.getMessage(), e);
			}
		}

	}
	
	/**
	 * 更新${modelComment !}
	 * @param ${modelName?uncap_first}
	 * @return
	 */
	@RequestMapping(value = "/update")
	public void update(${modelName?cap_first} ${modelName?uncap_first}) {
		JSONObject jsonObject = new JSONObject();
		try {
			if (${modelName?uncap_first} != null && ${modelName?uncap_first}.getId() != null) {
				${modelName?uncap_first}Service.updateById(${modelName?uncap_first}.getId());
			}
			jsonObject.put("sortMsg", "操作成功");
			jsonObject.put("state", StatusEnum.STATUS_SUCCESS.getIndex());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			jsonObject.put("sortMsg", "操作失败");
			jsonObject.put("state", StatusEnum.STATUS_ERROR.getIndex());
		} finally {
			try {
				printPageMsg(response, jsonObject.toJSONString());
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
	}

		
	/**
	 * 分页查询${modelComment !}
	 * @return
	 */
	@RequestMapping(value = "/pageList")
	@ResponseBody
	public GridResponse<${modelName?cap_first}> pageList(${modelName?cap_first} ${modelName}) {
	        GridResponse<${modelName?cap_first}> gridResponse = null;
		try {
		    if (${modelName} != null) {
			gridResponse = ${modelName}Service.getPage(${modelName});
		    }
		} catch (Exception e) {
		    logger.error(e.getMessage(), e);
		}
		return gridResponse;
	}
	
	/**
	 * 根据id查询${modelComment !}
	 * @return
	 */
	@RequestMapping(value = "/findById")
	@ResponseBody
	public ${modelName?cap_first} findById(String id) {
	        ${modelName?cap_first} ${modelName?uncap_first} = null;
		try {
		    if (StringUtils.isNotBlank(id)) {
			${modelName?uncap_first} = ${modelName}Service.findById(id);
		    }
		} catch (Exception e) {
		    logger.error(e.getMessage(), e);
		}
		return ${modelName?uncap_first};
	}
}